###############################################################################
## AUTHOR: ALAN YAN
## DATE LAST UPDATED: 03/28/2021
## PURPOSE: MARGINAL MEANS YOUGOV CONJOINT DATA 
###############################################################################
rm(list = ls())

#### PACKAGES ####
library(pacman)
p_load(tidyverse,
       estimatr,
       broom,
       haven,
       hrbrthemes,
       cjoint,
       formula.tools, 
       DeclareDesign,
       emmeans,
       lmtest,
       cregg,
       gridExtra,
       egg,
       car)

#### LOAD DATA ####
df <- read_rds("01-yougov-conjoint/01-data/cleaned/yougov-conjoint-cleaned.rds")

#### ANALYZE DATA (NO CONTROLS) ####
#Do people prefer firms with these features?
main_work_td <- mm(work_binary ~ 
                     as.factor(cje_corporate_gov) +
                     as.factor(cje_corporate_resp) +
                     as.factor(cje_firm_size) +
                     as.factor(cje_political_donations) + 
                     as.factor(cje_gender_ownership) + 
                     as.factor(cje_healthcare) + 
                     as.factor(cje_hours) + 
                     as.factor(cje_training) + 
                     as.factor(cje_location) + 
                     as.factor(cje_sick_leave) + 
                     as.factor(cje_parental_leave) + 
                     as.factor(cje_race_ownership) + 
                     as.factor(cje_retirement) + 
                     as.factor(cje_income) +
                     as.factor(cje_type_of_work) +
                     as.factor(cje_union) +
                     as.factor(cje_work_from_home) +
                     as.factor(cje_team_work) +
                     as.factor(cje_work_culture),
                   data = df,
                   id = df$rid,
) %>%
  select(
    `Attribute level` = level,
    Estimate = estimate,
    `Std. error` = std.error
  )

#Do people believe some firms are better at dealing with complaints?
main_complaints_td <- mm(complaints_binary ~ 
                           as.factor(cje_corporate_gov) +
                           as.factor(cje_corporate_resp) +
                           as.factor(cje_firm_size) +
                           as.factor(cje_political_donations) + 
                           as.factor(cje_gender_ownership) + 
                           as.factor(cje_healthcare) + 
                           as.factor(cje_hours) + 
                           as.factor(cje_training) + 
                           as.factor(cje_location) + 
                           as.factor(cje_sick_leave) + 
                           as.factor(cje_parental_leave) + 
                           as.factor(cje_race_ownership) + 
                           as.factor(cje_retirement) + 
                           as.factor(cje_income) +
                           as.factor(cje_type_of_work) +
                           as.factor(cje_union) +
                           as.factor(cje_work_from_home) +
                           as.factor(cje_team_work) +
                           as.factor(cje_work_culture),
                         data = df,
                         id = df$rid
)  %>%
  select(
    `Attribute level` = level,
    Estimate = estimate,
    `Std. error` = std.error
  )

#Do people believe they'll have more responsibilties at some firms?
main_responsibility_td <- mm(responsibility_binary ~ 
                               as.factor(cje_corporate_gov) +
                               as.factor(cje_corporate_resp) +
                               as.factor(cje_firm_size) +
                               as.factor(cje_political_donations) + 
                               as.factor(cje_gender_ownership) + 
                               as.factor(cje_healthcare) + 
                               as.factor(cje_hours) + 
                               as.factor(cje_training) + 
                               as.factor(cje_location) + 
                               as.factor(cje_sick_leave) + 
                               as.factor(cje_parental_leave) + 
                               as.factor(cje_race_ownership) + 
                               as.factor(cje_retirement) + 
                               as.factor(cje_income) +
                               as.factor(cje_type_of_work) +
                               as.factor(cje_union) +
                               as.factor(cje_work_from_home) +
                               as.factor(cje_team_work) +
                               as.factor(cje_work_culture),
                             data = df,
                             id = df$rid
) %>%
  select(
    `Attribute level` = level,
    Estimate = estimate,
    `Std. error` = std.error
  )

#Do people believe they'll have more power at some firms?
main_power_td <- mm(power_binary ~ 
                      as.factor(cje_corporate_gov) +
                      as.factor(cje_corporate_resp) +
                      as.factor(cje_firm_size) +
                      as.factor(cje_political_donations) + 
                      as.factor(cje_gender_ownership) + 
                      as.factor(cje_healthcare) + 
                      as.factor(cje_hours) + 
                      as.factor(cje_training) + 
                      as.factor(cje_location) + 
                      as.factor(cje_sick_leave) + 
                      as.factor(cje_parental_leave) + 
                      as.factor(cje_race_ownership) + 
                      as.factor(cje_retirement) + 
                      as.factor(cje_income) +
                      as.factor(cje_type_of_work) +
                      as.factor(cje_union) +
                      as.factor(cje_work_from_home) +
                      as.factor(cje_team_work) +
                      as.factor(cje_work_culture),
                    data = df,
                    id = df$rid
)  %>%
  select(
    `Attribute level` = level,
    Estimate = estimate,
    `Std. error` = std.error
  )

#### tables S40 to S51 ####
print(xtable::xtable(
  main_work_td,
  caption = "Replicate descriptive marginal means in Table A2 for work results",
  label = "tab:replicate-marginal-means-work"
    ), 
      include.rownames = FALSE)

print(xtable::xtable(
  main_power_td,
  caption = "Replicate descriptive marginal means in Table A2 for power results",
  label = "tab:replicate-marginal-means-power"
), 
include.rownames = FALSE)

print(xtable::xtable(
  main_responsibility_td,
  caption = "Replicate descriptive marginal means in Table A2 for responsibility results",
  label = "tab:replicate-marginal-means-responsibility"
), 
include.rownames = FALSE)

print(xtable::xtable(
  main_complaints_td,
  caption = "Replicate descriptive marginal means in Table A2 for complaint results",
  label = "tab:replicate-marginal-means-complaint"
), 
include.rownames = FALSE)

